package com.example.mycarcalling.service.impl;

import cn.dev33.satoken.stp.StpUtil;
import cn.hutool.log.Log;
import com.example.mycarcalling.bean.Netease;
import com.example.mycarcalling.bean.Order;
import com.example.mycarcalling.bean.User;
import com.example.mycarcalling.dao.NeteaseMapper;
import com.example.mycarcalling.dao.OrderMapper;
import com.example.mycarcalling.dao.UserMapper;
import com.example.mycarcalling.service.UserService;
import com.sun.org.apache.xpath.internal.operations.Or;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import sun.security.krb5.internal.PAData;

import javax.annotation.Resource;
import java.util.Date;

@Service("userService")
@Transactional(rollbackFor = Exception.class)
public class UserServiceImpl implements UserService {

    @Resource
    private UserMapper userMapper;

    @Resource
    private NeteaseMapper neteaseMapper;

    @Resource
    private OrderMapper orderMapper;

    @Override
    public User login(String account, String password) {
        try {
            return userMapper.selectUserByAccountAndPwd(account,password);
        }catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    @Override
    public Long register(User user) {
        try {
            if(null==userMapper.selectUserByAccount(user.getAccount())){
                userMapper.insertSelective(user);
                if(null!=user.getId()){

                }
                Netease netease = new Netease();
                netease.setImUser(user.getId());
                netease.setImToken(user.getPassword());
                if(0<neteaseMapper.insertSelective(netease)){
                    return user.getId();
                }
                return null;
            }else{
                return null;
            }
        }catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    @Override
    public User findUserByTokenValue(String tokenValue) {
        try {
            System.out.println(StpUtil.getTokenValue()+"    and    "+tokenValue);
            if(tokenValue.equals(StpUtil.getTokenValue())) {
                return userMapper.selectByPrimaryKey(StpUtil.getLoginIdAsLong());
            }else {
                throw new Exception();
            }
        }catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    @Override
    public User findUserById(Long userId) {
        if(userId ==0){
            return null;
        }
        User user = null;
        try {
            user = userMapper.selectByPrimaryKey(userId);
            return user;
        }catch (Exception e){
            e.printStackTrace();
            return null;
        }
    }

    @Override
    public int updateUserInfo(User user) {
        if(null == user){
            return 0;
        }
        try {
            return userMapper.updateByPrimaryKeySelective(user);
        }catch (Exception e) {
            e.printStackTrace();
            return 0;
        }
    }

    @Override
    public int updateLoginTime() {
        try {
            User user = new User();
            user.setId(StpUtil.getLoginIdAsLong());
            user.setLoginTime(new Date());
            return userMapper.updateByPrimaryKeySelective(user);
        }catch (Exception e){
            e.printStackTrace();
            return -1;
        }
    }

    @Override
    public boolean IsDriver(Long userid) {
        int isDriver;
        try {
            isDriver = userMapper.selectByPrimaryKey(userid).getIsDriver();
            System.out.println("isDriver:"+isDriver);
            if(isDriver == 1){
                return true;
            }
            return false;
        }catch (Exception e){
            e.printStackTrace();
            return false;
        }
    }

    @Override
    public String getDriver(Long passengerId) {
        if(0 == passengerId){
            return null;
        }
        String driverUserName = null;
        Order order = null;
        try {
            order = orderMapper.selectPassengerOrderOnDriving(passengerId);
            if(null == order){
                return null;
            }
            driverUserName = userMapper.selectByPrimaryKey(order.getDeliveryDriver()).getAccount();
            return driverUserName;

        }catch (Exception e){
            e.printStackTrace();
            return null;
        }
    }

    @Override
    public User getPassenger(long driverId) {
        if(0 == driverId){
            return null;
        }
        Order order = null;
        try {
            order = orderMapper.selectOrdersOnDriving(driverId);
            if(null == order){
                return null;
            }
            return  userMapper.selectByPrimaryKey(order.getTravelPassenger());

        }catch (Exception e){
            e.printStackTrace();
            return null;
        }
    }

    @Override
    public int changePassword(User updateUser) {
        if(null == updateUser){
            return 0;
        }
        try {
            int result = userMapper.updateByPrimaryKeySelective(updateUser);
            System.out.println("resultId:" +result);
            return result;
        }catch (Exception e){
            e.printStackTrace();
            return 0;
        }
    }
}
